# Copyright 2024 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""ut for file system"""
import pytest

from mindspore.common.file_system import FileSystem, _register_basic_file_system, _register_mindio_file_system, \
    set_mindio_server_info, mindio_preload


def test_register_file_system():
    """
    Feature: Test register file system.
    Description: Try to register default file system and mindio file system.
    Expectation: Success.
    """
    fs = FileSystem()
    ret = _register_basic_file_system(fs)
    assert ret is True
    assert fs.create == open  # pylint: disable=comparison-with-callable
    assert fs.create_args == ("ab",)
    assert fs.open == open  # pylint: disable=comparison-with-callable
    assert fs.open_args == ("rb",)

    ret = _register_mindio_file_system(fs)
    # MindIO module is not installed in ut environment
    assert ret is False


def test_mindio_func():
    """
    Feature: Validate MindIO File System.
    Description: Test error handling and edge cases in MindIO setup.
    Expectation: Success.
    """
    with pytest.raises(ValueError):
        set_mindio_server_info(-200)
    with pytest.raises(TypeError):
        mindio_preload(100)
    res = set_mindio_server_info(200)
    assert res is None
    res = mindio_preload("./tmp.ckpt")
    assert res is False
